#include "snake.h"
#include "./ui_snake.h"
#include<QPainter>
#include<qtimer.h>
#include<QKeyEvent>
#include <QRandomGenerator>

snake::snake(QWidget* parent)
    : QMainWindow(parent)
    , blsRun(false), speed(500)
{
    this->setGeometry((QRect(600, 300, 290, 310)));
}

snake::~snake()
{
    delete ui;
}
void snake::paintEvent(QPaintEvent* event) {
    QPainter painter(this);
    if (!blsRun) {
        InitSnake();
    }


    //外墙
    painter.setPen(Qt::black);
    painter.setBrush(Qt::gray);
    painter.drawRect(15, 15, 260, 260);

    //内墙
    painter.setPen(Qt::black);
    painter.setBrush(Qt::black);
    painter.drawRect(20, 20, 250, 250);
    painter.drawPixmap(20, 20, 250, 250, QPixmap(":/snake/image/zhen.jpg"));

    //画格子
    /*painter.setPen(Qt::blue);
    for(int i=2;i<=27;i++){
        painter.drawLine(20,i*10,270,i*10);//画横线
        painter.drawLine(i*10,20,i*10,270);//画竖线
    }*/
    //显示游戏的开始与结束
    QFont font1("Courier", 25);
    painter.setFont(font1);
    painter.setPen(Qt::red);
    painter.setBrush(Qt::red);
    painter.drawText(40, 150, sDisplay);
    //得分显示
    QFont font2("Courier", 15);
    painter.setFont(font2);
    painter.setPen(Qt::blue);
    painter.setBrush(Qt::blue);
    painter.drawText(140, 300, scoreLabel);
    painter.drawText(230, 300, QString::number(nScore));
    //画蛇
    painter.setPen(Qt::black);
    painter.setBrush(Qt::green);
    painter.drawRect(food);
    painter.drawRects(&vSnakeRect[0], vSnakeRect.size());
    if (blsOver) {//游戏结束
        timer->stop();
    }
}
void snake::InitSnake() {
    nDirection = 2;
    blsRun = true;
    blsOver = false;
    sDisplay = "Start!";
    scoreLabel = "score:";
    nScore = 0;
    food = CreateRect();//创建食物
    //QRect rect(100,70,10,10);//蛇的初始位置
    //vSnakeHead=rect;//将矩形赋值给蛇
    vSnakeRect.resize(5);//初始化snake的长度是5
    //利用一个for循环初始化vector
    for (int i = 0; i < vSnakeRect.size(); i++) {
        QRect rect(100, 70 + 10 * i, 10, 10);
        vSnakeRect[vSnakeRect.size() - 1 - i] = rect;
    }
    SnakeHead = vSnakeRect.first();
    timer = new QTimer(this);//设定计时器
    timer->start(speed);//设定时间500毫秒
    connect(timer, SIGNAL(timeout()), SLOT(Snake_update()));

}
void snake::Snake_update() {
    sDisplay = "";
    SnakeHead = vSnakeRect.first();//获取到蛇头
    IsEat();
    IsHit();
    //让数组移动，除了蛇头，后面的到前面的位置，因为蛇头是控制方向的
    for (int j = 0; j < vSnakeRect.size() - 1; j++) {
        vSnakeRect[vSnakeRect.size() - 1 - j] = vSnakeRect[vSnakeRect.size() - 2 - j];
    }
    switch (nDirection) {
    case 1:
        SnakeHead.setTop(SnakeHead.top() - 10);
        SnakeHead.setBottom(SnakeHead.bottom() - 10);
        break;
    case 2:
        SnakeHead.setTop(SnakeHead.top() + 10);
        SnakeHead.setBottom(SnakeHead.bottom() + 10);
        break;
    case 3:
        SnakeHead.setLeft(SnakeHead.left() - 10);
        SnakeHead.setRight(SnakeHead.right() - 10);
        break;
    case 4:
        SnakeHead.setLeft(SnakeHead.left() + 10);
        SnakeHead.setRight(SnakeHead.right() + 10);
        break;
    default:;
    }
    vSnakeRect[0] = SnakeHead;//第一个节点设置为蛇头位置
    if (SnakeHead.left() < 20 || SnakeHead.right() > 270 || SnakeHead.top() < 20 || SnakeHead.bottom() > 270) {
        sDisplay = "Game over!";
        blsOver = true;
    }
    update();//paintEvent更新
}
void snake::keyPressEvent(QKeyEvent* event) {
    QKeyEvent* key = (QKeyEvent*)event;
    switch (key->key()) {
    case Qt::Key_Up: nDirection = 1;
        break;
    case Qt::Key_Down:nDirection = 2;
        break;
    case Qt::Key_Left: nDirection = 3;
        break;
    case Qt::Key_Right: nDirection = 4;
        break;
    default:;
    }
}
QRect snake::CreateRect() {
    int x, y;
    x = QRandomGenerator::global()->bounded(0, 1000) % 25;
    y = QRandomGenerator::global()->bounded(0, 1000) % 25;
    QRect rect(20 + x * 10, 20 + y * 10, 10, 10);//食物小方块位置
    return rect;
}
void snake::IsEat() {
    if (SnakeHead == food) {
        SnakeHead = food;//吃到食物变蛇头
        vSnakeRect.push_back(vSnakeRect.last());//将最后一个节点再次加到容器里，让蛇身变长
        food = CreateRect();//吃到最后一个食物再生成一个食物
        nScore += 20;//得分
        if (speed > 50) {
            speed = speed - 10;
            timer->stop();
            timer->start(speed);
        }
    }
}
void snake::IsHit() {
    for (int i = 1; i < vSnakeRect.size(); i++) {
        if (SnakeHead == vSnakeRect[i]) {
            sDisplay = "Game over!";
            blsOver = true;
            update();
        }
    }
}